package cn.col.gszf.service.impl;

import cn.col.gszf.mapper.VotesMapper;
import cn.col.gszf.pojo.Vote;
import cn.col.gszf.service.VoteService;
import cn.col.gszf.service.RedisService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wanghong
 * @created 2024-06-23 22:49
 * @description 投票实现类
 **/
@Service
public class VoteServiceImpl implements VoteService {

    @Resource
    private VotesMapper votesMapper;

    @Resource
    private RedisService redisService;

    @Override
    public List<Vote> findAllVotes() {
        return votesMapper.selectList(null);
    }

    @Override
    public Vote findById(Integer id) {
        List<Vote> list = votesMapper.selectList(new QueryWrapper<Vote>().eq("id", id));
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public void deleteVoteById(Integer id) {
        votesMapper.deleteById(id);
        redisService.remove("option_" + id);
    }
}
